/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.ampathhousehold.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.module.ampathhousehold.dao.HouseholdDAO;
import org.openmrs.module.ampathhousehold.model.Household;

/**
 *
 */
public class HibernateHouseholdDAO implements HouseholdDAO {
	
	private SessionFactory sessionFactory;
	
	/**
	 * Set the sessionFactory with the sessionFactory value
	 * 
	 * @param sessionFactory the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/**
	 * @see org.openmrs.module.ampathhousehold.dao.HouseholdDAO#getAllHouseholds()
	 */
	@SuppressWarnings("unchecked")
    @Override
	public List<Household> getAllHouseholds() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Household.class);
		return criteria.list();
	}
	
	/**
	 * @see org.openmrs.module.ampathhousehold.dao.HouseholdDAO#getHousehold(java.lang.Integer)
	 */
	@Override
	public Household getHousehold(Integer id) {
		return (Household) sessionFactory.getCurrentSession().get(Household.class, id);
	}
	
	/**
	 * @see org.openmrs.module.ampathhousehold.dao.HouseholdDAO#getHouseholds(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
    @Override
	public List<Household> getHouseholds(String village) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Household.class);
		criteria.add(Restrictions.eq("village", village));
		return criteria.list();
	}
	
	/**
	 * @see org.openmrs.module.ampathhousehold.dao.HouseholdDAO#saveHousehold(org.openmrs.module.ampathhousehold.model.Household)
	 */
	@Override
	public Household saveHousehold(Household household) {
		sessionFactory.getCurrentSession().saveOrUpdate(household);
		return household;
	}
	
}
